package et.six.eta.View;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import et.six.eta.R;

public class ReflashListview extends ListView implements OnScrollListener {

    View Header;//顶部布局文件
    int headerHeight;//顶部布局文件的高度
    int scrollState;//listview当前滚动状态
    int i;//当前第一个可见的item的位置
    boolean isRemark;//标记，当前是在list view最顶端按下的
    int startY;//按下时的Y值

    int state;//当前状态
    final int NONE=0;//正常状态
    final int PULL=1;//提示下拉状态
    final int RELESE=2;//提示释放状态
    final int REFLASHING=3;//刷新
    IReflashListener iReflashListener;//刷新数据的接口

    public ReflashListview(Context context) {
        super(context);
        initView(context);
    }

    public ReflashListview(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ReflashListview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
    /*
     * 初始化界面，添加顶部布局文件到listview
     * */
    private void initView(Context context){
        LayoutInflater inflater = LayoutInflater.from(context);
        Header=inflater.inflate(R.layout.header_layout,null);
        measureView(Header);
        headerHeight=Header.getMeasuredHeight();
        toppadding(-headerHeight);
        this.addHeaderView(Header);
        this.setOnScrollListener(this);
    }
    /*
     * 通知父布局占用的宽，高
     * */
    private void measureView(View view){
        ViewGroup.LayoutParams p=view.getLayoutParams();
        if (p==null){
            p=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        int width=ViewGroup.getChildMeasureSpec(0,0,p.width);
        int height;
        int tempHeight=p.height;
        if (tempHeight>0){
            height=MeasureSpec.makeMeasureSpec(tempHeight,MeasureSpec.EXACTLY);
        }else {
            height=MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        }
        view.measure(width,height);
    }

    /*
     * 设置布局的上边距
     * */
    private void toppadding(int toppadding){
        Header.setPadding(Header.getPaddingLeft(),toppadding,
                Header.getPaddingRight(),Header.getPaddingBottom());
        Header.invalidate();
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState ) {
        this.scrollState=scrollState;
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        this.i=i;

    }
    //触摸事件
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:   //按下
                if (i==0){
                    isRemark=true;
                    startY= (int) ev.getY();
                }
                break;
            case MotionEvent.ACTION_MOVE:   //移动
                OnMove(ev);
                break;
            case MotionEvent.ACTION_UP:     //松开时
                if (state==RELESE){//提示下拉刷新
                    state=REFLASHING;//状态变正在刷新，加载数据
                    //加载最新数据
                    reflashViewByState();
                    iReflashListener.onReflash();//调用接口
                }else if (state==PULL){
                    state=NONE;
                    isRemark=false;
                    reflashViewByState();
                }
                break;
        }
        return super.onTouchEvent(ev);
    }
    //判断移动时操作
    private void OnMove(MotionEvent ev){
        if (!isRemark){
            return;
        }
        int tempY= (int) ev.getY();//当前Y值
        int space=tempY-startY;//移动的Y值
        int toppadding=space-headerHeight;
        switch(state){
            case NONE:  //正常
                if (space>0){
                    state=PULL;
                    reflashViewByState();
                }
                break;
            case PULL:  //下拉
                toppadding(toppadding);
                if (space>headerHeight+30 &&scrollState==SCROLL_STATE_TOUCH_SCROLL){
                    state=RELESE;
                    reflashViewByState();
                }
                break;
            case RELESE:    //释放
                toppadding(toppadding);
                if (space<headerHeight+30){
                    state=PULL;//变成下拉状态
                    reflashViewByState();

                }else if (space<=0){
                    state=NONE;
                    isRemark=false;
                    reflashViewByState();
                }
                break;
        }
    }
    //根据当前状态，改变界面
    private void reflashViewByState(){
        TextView tip=Header.findViewById(R.id.tv_tip);
        ImageView arrow=Header.findViewById(R.id.arrow);
        ProgressBar progress=Header.findViewById(R.id.progress);
        //鸡腿方向改变，转体180
        RotateAnimation anim=new RotateAnimation(0,180,
                RotateAnimation.RELATIVE_TO_SELF,0.5f,
                RotateAnimation.RELATIVE_TO_SELF,0.5f);
        anim.setDuration(500);//时间间隔
        anim.setFillAfter(true);

        RotateAnimation anim1=new RotateAnimation(180,0,
                RotateAnimation.RELATIVE_TO_SELF,0.5f,
                RotateAnimation.RELATIVE_TO_SELF,0.5f);
        anim1.setDuration(500);
        anim1.setFillAfter(true);

        switch (state){
            case NONE:
                toppadding(-headerHeight);
                arrow.clearAnimation();//移出动画
                break;
            case PULL:
                arrow.setVisibility(View.VISIBLE);
                progress.setVisibility(View.GONE);
                tip.setText("下拉可以刷新");
                arrow.clearAnimation();
                arrow.setAnimation(anim);
                break;
            case RELESE:
                arrow.setVisibility(View.VISIBLE);
                progress.setVisibility(View.GONE);
                tip.setText("松开可以刷新");
                arrow.clearAnimation();//移出动画
                arrow.setAnimation(anim1);//改变鸡腿方向
                break;
            case REFLASHING:
                toppadding(40);
                arrow.setVisibility(View.GONE);
                progress.setVisibility(View.VISIBLE);
                tip.setText("正在刷新...");
                arrow.clearAnimation();//移出动画
                break;
        }
    }

    public void setInterface(IReflashListener iReflashListener){
        this.iReflashListener=iReflashListener;
    }
    //刷新数据接口
    public interface IReflashListener{
        void onReflash();
    }
}
